package com.base.rabbit.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfiguration {

    @Value("${rabbit.jdbc.driver-class-name}")
    private String jdbcDriverClassName ;
    @Value("${rabbit.jdbc.url}")
    private String jdbcUrl ;
    @Value("${rabbit.dbc.username}")
    private String jdbcUsername ;
    @Value("${rabbit.jdbc.password}")
    private String jdbcPassword ;
    @Value("${rabbit.jdbc.initial-size:1}")
    private Integer jdbcInitialSize ;
    @Value("${rabbit.jdbc.min-idle:3}")
    private Integer jdbcMinIdle ;
    @Value("${rabbit.jdbc.max-idle:10}")
    private Integer jdbcMaxIdle ;
    @Value("${rabbit.jdbc.max-wait:60000}")
    private Integer jdbcMaxWait ;
    @Value("${rabbit.jdbc.max-active:20}")
    private Integer jdbcMaxActive ;

    @Bean(destroyMethod="close")
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(jdbcUrl);
        dataSource.setDriverClassName(jdbcDriverClassName);
        dataSource.setPassword(jdbcPassword);
        dataSource.setUsername(jdbcUsername);
        dataSource.setMinIdle(jdbcMinIdle);
        dataSource.setMaxActive(jdbcMaxActive);
        dataSource.setMaxIdle(jdbcMaxIdle);
        dataSource.setMinEvictableIdleTimeMillis(30000);
        dataSource.setValidationQuery("select 'x'");
        dataSource.setInitialSize(jdbcInitialSize);
        dataSource.setMaxWait(jdbcMaxWait);
        dataSource.setTestOnBorrow(false);
        dataSource.setTestOnReturn(false);
        dataSource.setPoolPreparedStatements(false);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
        dataSource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000");
        return dataSource;
    }

}